<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Redis\Channel;

/**
 * Class ManyiController
 * @package Pay\Controller
 */
class DuoshanController extends PayController{
    protected $config;
	protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = [];
        $data['merCode'] = $this->config['mch_id'];
        switch($this->config['paytype']) {
            case 1:
                $data['payType'] = 'WX';
                break;
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                $data['payType'] = 'ZFB';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['orderID'] = $return['orderid'];
        $data['orderAmount'] = number_format($return['amount'],2,'.','');
        $data['orderComment'] = '7309';
        $data['noticeURL'] = $return['notifyurl'];
        $data['version'] = 'V2.0';

        $data['sign'] = $this->getSign($data);

        $res = curl($this->config['gateway'], httpstr($data));
        $res = json_decode($res, true);

        /*echo "<pre/>"; print_r($data);
        echo "请求地址：" . $this->config['gateway'] . '<br/>';
        print_r($res); exit;*/

        $code = $res['retData']['payeeQRCodeURL'];
        if(!$code){
            if($res['retMsg']){
                $this->showmessage($res['retMsg'],['code'=>C("THIRDCODES.".$this->class)]);
            }
            $this->showmessage('暂不支持获取支付链接失败此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        //$code = "http://47.89.32.230:9998/otcAPI/" . $code;

        //header("Location:{$code}"); exit;

        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("code_url", $code);
        $this->assign("img_width", "500px");
        $this->assign("img_height", "500px");
        switch($this->config['paytype']){
            //微信
            case 1:
                if(!isMobile()){
                    $this->display("WeiXin/weixin");
                }else{
                    $this->display("WeiXin/weixinwap");
                }

                break;
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:

                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    //加密
    private function getSign($data){
        $str = $data['merCode'] . '|' . $data['payType'] . '|' . $data['orderID'] . '|' . $this->config['signkey'];

        return md5($str);
    }

    public function notifyurl(){
        //$this->debug_callback();
        if(!$this->checkip(getIP(),['47.74.178.125'])){
            exit("wrong");
        }

        $p = I("post.");
        //$p = array ( 'merCode' => 'A123456', 'payType' => 'WX', 'orderAmount' => '100.0', 'orderComment' => '7309', 'orderID' => 'sn20190709213040899', 'sign' => '1f231ffd881c642a89bf1773fcbae64b', 'ackedAmount' => '100.00', 'seqID' => '20190709220759375F458438B', );

        if(empty($p)){
            exit("");
        }

        //获取订单信息
        $orderid = $p['orderID'];
        $find = $this->checkOrderNotify($orderid);

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "pay_orderid_rd"=> htmlspecialchars($p['seqID']),
            'pay_true' => floatval($p['ackedAmount']),
        ));

        if(($find['pay_amount'] - $p['ackedAmount']) >= 1){
            exit("money_not_match");
        }

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();


        $sign = $p['sign'];
        $sign2 = $this->getSign($p);

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        //处理支付
        $this->async($find['pay_orderid'], "SUCCESS");
    }
}